package cn.xfdzx.android.apps.shop.activity.remake;



import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.MainActivity;
import cn.xfdzx.android.apps.shop.adapter.remake.ShopRemake_MarketAdapter;
import cn.xfdzx.android.apps.shop.adapter.remake.ShopRemake_RetailAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadMoreImgBean;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadSingImgBean;
import cn.xfdzx.android.apps.shop.bean.DissatisfiedReasonBean;
import cn.xfdzx.android.apps.shop.bean.MyRemakeListMarketBean;
import cn.xfdzx.android.apps.shop.bean.MyRemakeListRetailBean;
import cn.xfdzx.android.apps.shop.bean.PeiSongBeen;
import cn.xfdzx.android.apps.shop.bean.ShopRemakeListMarketBean;
import cn.xfdzx.android.apps.shop.bean.ShopRemakeListRetailBean;
import cn.xfdzx.android.apps.shop.contract.OrderRemakeContract;
import cn.xfdzx.android.apps.shop.presenter.OrderRemakePresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.view.MyRatingBar;

/*批发店铺评价页面*/
public class ShopRemake_MarketActivity extends BaseMvpActivity<OrderRemakePresenter>implements OrderRemakeContract.View , View.OnClickListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;      //标题栏-左边按钮
    @BindView(R.id.recycle_remake)
    RecyclerView recycle_remake;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private ShopRemake_MarketAdapter marketAdapter;//批发店铺评价适配器
    private int page = 1;
    private int limit = Constants.LIMIT;
    private String storeId;//店铺id
    private String goodsId;//商品id
    private int isContent = 2;//是否只查看有内容的评价 2=>有内容的评价 1 => 全部
    private MyRatingBar ratingBar;      //
    private TextView tv_haoping;//好评率
    private TextView tv_remake_all;//全部
    private TextView tv_remake_havecontent;//有内容
    private View viewleft,viewright;
    private ImageView star01,star02,star03,star04,star05;
    private List<ShopRemakeListMarketBean.DataBean> data = new ArrayList<>();
    private View emptyView;
    private List<ShopRemakeListMarketBean.DataBean.ReplyBean> reply = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_remake_shopretail;
    }

    @Override
    public void initView() {
        storeId = getIntent().getStringExtra("storeId");
        goodsId = getIntent().getStringExtra("goodsId");
        mPresenter = new OrderRemakePresenter();
        mPresenter.attachView(this);

        //请求列表
        getShopRemarketList();

        initRecycle();
    }

    private void initRecycle() {
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;

                getShopRemarketList();

            }
        });
        recycle_remake.setLayoutManager(new LinearLayoutManager(this));
        View headView = LayoutInflater.from(this).inflate(R.layout.activity_remake_shopmarket_head, recycle_remake, false);
        ratingBar = headView.findViewById(R.id.ratingBar);
        tv_haoping = headView.findViewById(R.id.tv_haoping);
        tv_remake_all = headView.findViewById(R.id.tv_remake_all);
        tv_remake_havecontent = headView.findViewById(R.id.tv_remake_havecontent);
        viewleft = headView.findViewById(R.id.viewleft);
        viewright = headView.findViewById(R.id.viewright);
//        star01 = headView.findViewById(R.id.star01);
//        star02 = headView.findViewById(R.id.star02);
//        star03 = headView.findViewById(R.id.star03);
//        star04 = headView.findViewById(R.id.star04);
//        star05 = headView.findViewById(R.id.star05);

        marketAdapter = new ShopRemake_MarketAdapter(reply);
        recycle_remake.setAdapter(marketAdapter);
        marketAdapter.addHeaderView(headView);

        emptyView = getLayoutInflater().inflate(R.layout.view_emptyview, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        marketAdapter.setHeaderAndEmpty(true);
        marketAdapter.setEmptyView(emptyView);

        tv_remake_all.setOnClickListener(this);
        tv_remake_havecontent.setOnClickListener(this);
    }

    private void getShopRemarketList() {
        Map<String,Object> map = new HashMap<>();
        map.put("storeId",storeId);
        map.put("limit",limit);
        map.put("page",page);
        map.put("goodsId",goodsId);
        map.put("isContent",isContent);
        mPresenter.getShopRemakeMarket(true,true,map);
    }


    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onUploadMoreImgSuccess(AfterServiceUpLoadMoreImgBean bean) {

    }

    @Override
    public void onUploadSingImgSuccess(AfterServiceUpLoadSingImgBean bean) {

    }

    @Override
    public void onGetDissatisfiedSuccess(DissatisfiedReasonBean bean) {

    }

    @Override
    public void onGetRemakeRetailSuccess(DissatisfiedReasonBean bean) {

    }

    @Override
    public void getPeisongInfoSuccess(PeiSongBeen bean) {

    }

    @Override
    public void onGetRemakeMarketSuccess(DissatisfiedReasonBean bean) {

    }

    @Override
    public void onGetMyRemakeListRetailSuccess(MyRemakeListRetailBean bean) {
    }

    @Override
    public void onGetMyRemakeListMarketSuccess(MyRemakeListMarketBean bean) {
    }

    @Override
    public void onGetDeleteRemakeSuccess(MyRemakeListMarketBean bean) {
    }

    @Override
    public void onGetShopRemakeRetailSuccess(ShopRemakeListRetailBean bean) {

    }

    @Override
    public void onGetShopRemakeMarketSuccess(ShopRemakeListMarketBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (EmptyUtil.isNotEmpty(bean.getData().getCount())){
                tv_haoping.setText(bean.getData().getCount().getPraiseRate());
                ratingBar.setRating(Float.parseFloat(bean.getData().getCount().getStart()));
                if (bean.getData().getCount().getIsContentCount()>9999){
                    tv_remake_havecontent.setText("有内容的评价(9999+)");
                }else {
                    tv_remake_havecontent.setText("有内容的评价("+bean.getData().getCount().getIsContentCount()+")");
                }

                if (bean.getData().getCount().getAllCount()>9999){
                    tv_remake_all.setText("全部评价(9999+)");
                }else {
                    tv_remake_all.setText("全部评价("+bean.getData().getCount().getAllCount()+")");
                }


            }
            if (page == 1) {
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        Map<String,Object> map = new HashMap<>();
                        map.put("storeId",storeId);
                        map.put("limit",limit);
                        map.put("page",page);
                        map.put("goodsId",goodsId);
                        map.put("isContent",isContent);
                        mPresenter.getShopRemakeMarket(false,true,map);
                    }
                });
            }

            if (page == 1){
                mRefreshLayout.setNoMoreData(false);
                reply.clear();
                if (EmptyUtil.isNotEmpty(bean.getData().getReply())){
                    reply = bean.getData().getReply();
                    marketAdapter.refreshDatas(reply);
                }else {
                    marketAdapter.clearDatas();
                }
            }else {
                if (EmptyUtil.isNotEmpty(bean.getData().getReply())){
                   // reply.addAll(bean.getData().getReply());
                    marketAdapter.addData(bean.getData().getReply());
                }
            }

            if (EmptyUtil.isEmpty(bean.getData().getReply())) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
            }

        }else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }


    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_remake_all:    //全部评价
                if (Utils.isFastClick()){
                    isContent = 1;
                    tv_remake_all.setTextColor(getResources().getColor(R.color.color_02C557));
                    tv_remake_havecontent.setTextColor(getResources().getColor(R.color.color_393939));

                    viewleft.setVisibility(View.INVISIBLE);
                    viewright.setVisibility(View.VISIBLE);
                    page = 1;
                    getShopRemarketList();
                }
                break;
            case R.id.tv_remake_havecontent:    //有内容评价
                if (Utils.isFastClick()){
                    isContent = 2;
                    tv_remake_all.setTextColor(getResources().getColor(R.color.color_393939));
                    tv_remake_havecontent.setTextColor(getResources().getColor(R.color.color_02C557));

                    viewleft.setVisibility(View.VISIBLE);
                    viewright.setVisibility(View.INVISIBLE);
                    page = 1;
                    getShopRemarketList();
                }
                break;

        }
    }
}
